package com.liebaut.services;

import com.liebaut.dao.util.UniqueNumberRepository;
import com.liebaut.model.util.UniqueNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * User: User
 * Date: 11/01/14
 * Time: 13:49
 * To change this template use File | Settings | File Templates.
 */
@Service
public class IDGeneratorService {

    @Autowired
    UniqueNumberRepository uniqueNumberRepository;

    public synchronized String getNextNumber(String name){
        UniqueNumber number = uniqueNumberRepository.findOneByDescription(name);
        long toRet = number.getNumber();
        number.setNumber(toRet+1);
        uniqueNumberRepository.save(number);
        return  number.getPrefix() + toRet;
    }
}
